package cn.csc.module.shoppingCard.api.transaction;

import cn.csc.framework.common.pojo.CommonResult;
import cn.csc.framework.common.util.collection.CollectionUtils;
import cn.csc.framework.security.core.util.SecurityFrameworkUtils;
import cn.csc.module.shoppingCard.api.transaction.dto.CardTransactionCreateReqDTO;
import cn.csc.module.shoppingCard.api.transaction.dto.CardTransactionRespDTO;
import cn.csc.module.shoppingCard.convert.transaction.CardTransactionConvert;
import cn.csc.module.shoppingCard.dal.dataobject.card.CardDO;
import cn.csc.module.shoppingCard.dal.dataobject.transaction.CardTransactionDO;
import cn.csc.module.shoppingCard.dal.mysql.card.CardMapper;
import cn.csc.module.shoppingCard.service.transaction.CardTransactionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.csc.module.shoppingCard.enums.ApiConstants.VERSION;

/**
 * @author liuchuang
 * @version 1.0
 * @modify
 * @since 2025/3/12 10:09
 */
@RestController // 提供 RESTful API 接口，给 Feign 调用
@DubboService(version = VERSION) // 提供 Dubbo RPC 接口，给 Dubbo Consumer 调用
@Validated
@Slf4j
public class CardTransactionApiImpl implements CardTransactionApi {

    @Resource
    private CardTransactionService cardTransactionService;

    @Resource
    private CardMapper cardMapper;

    @Override
    public CommonResult<Boolean> create(@RequestBody List<CardTransactionCreateReqDTO> list) {
        log.info("create 创建交易记录参数: {}",list);
        List<CardTransactionDO> transactionDOS = CardTransactionConvert.INSTANCE.convertList1(list);
        List<BigInteger> cardIds = transactionDOS.stream().map(CardTransactionDO::getCardId).collect(Collectors.toList());
        List<CardDO> cardDOS = cardMapper.selectListByIds(cardIds);
        log.info("create cardDOS: {}",cardDOS);
        Map<BigInteger, CardDO> doMap = CollectionUtils.convertMap(cardDOS, CardDO::getId);
        transactionDOS.forEach(t -> {
            t.setCardNumber(doMap.get(t.getCardId()).getCardNumber());
            t.setCreator(String.valueOf(SecurityFrameworkUtils.getLoginUserId()));
            t.setProjectId(doMap.get(t.getCardId()).getProjectId());
        });
        log.info("create transactionDOS: {}",transactionDOS);
        return CommonResult.success(cardTransactionService.saveBatch(transactionDOS));
    }

    @Override
    public CommonResult<Boolean> updateStatus(@RequestParam("orderIds") List<BigInteger> orderIds) {
        cardTransactionService.updateStatus(orderIds);
        return CommonResult.success();
    }

    @Override
    public CommonResult<List<CardTransactionRespDTO>> getByOrderIds(List<BigInteger> orderIds) {
        return CommonResult.success(CardTransactionConvert.INSTANCE.convertList03(cardTransactionService.getCardTransactList(orderIds)));
    }


}
